package com.ljl.springboot.service;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljl.springboot.common.Constants;
import com.ljl.springboot.controller.dto.UserDTO;
import com.ljl.springboot.entity.Menu;
import com.ljl.springboot.entity.User;
import com.ljl.springboot.exception.ServiceException;
import com.ljl.springboot.mapper.RoleMapper;
import com.ljl.springboot.mapper.RoleMenuMapper;
import com.ljl.springboot.mapper.UserMapper;
import com.ljl.springboot.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuService menuService;

    public boolean saveUser(User user){
        return saveOrUpdate(user);
    }

    public UserDTO login(UserDTO userDTO) {
        User one=getUserInfo(userDTO);
        if (one != null) {
            BeanUtil.copyProperties(one, userDTO, true);
            String token=TokenUtils.genToken(one.getId().toString(),one.getPassword());
            userDTO.setToken(token);

            String role=one.getRole();  //ROLE-ADMIN

            //设置用户的菜单列表
            List<Menu> roleMenus = getRoleMenus(role);
            userDTO.setMenus(roleMenus);
            return userDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
        }
    }
//        List<User> list = list(queryWrapper);
//        return list.size()!=0;

    public User register(UserDTO userDTO) {
        User one=getUserInfo(userDTO);
        if(one==null){
            one=new User();
            BeanUtil.copyProperties( userDTO,one, true);
            save(one);
        }else{
            throw new ServiceException(Constants.CODE_600,"用户已存在");
        }
        return null;
    }

    private User getUserInfo(UserDTO userDTO){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDTO.getUsername());
        queryWrapper.eq("password", userDTO.getPassword());
//        User one = getOne(queryWrapper);
//        return one!=null;   //有脏数据（重复账号信息）时不适用,也可以抛出异常
        User one;
        try {
            one = getOne(queryWrapper);
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }


    //获取当前角色的菜单列表
    private List<Menu> getRoleMenus(String flag){

        Integer roleId=roleMapper.selectOneByFlag(flag);
        //当前用户菜单集合
        List<Integer> menuIds=roleMenuMapper.selectByRoleId(roleId);

        //查出系统所有的菜单
        List<Menu> menus = menuService.findMenus("");

        List<Menu> roleMenus=new ArrayList<>();
        //筛选当前用户的菜单
        for (Menu menu : menus) {
            if (menuIds.contains(menu.getId())){
                roleMenus.add(menu);
            }
            List<Menu> children= menu.getChildren();
            children.removeIf(child->!menuIds.contains(child.getId()));
        }
        return roleMenus;
    }

//    @Autowired
//    private UserMapper userMapper;

//    public int save(User user){
//        if(user.getId()==null){
//            return userMapper.insert(user);        //user没有id表示是新增
//        }else{
//            return userMapper.update(user);        //user有id表示是更新
//        }
//    }


}
